<html lang="en-US">

<head>
    
<title>「连载四」Gin搭建Blog API&#39;s （三） - 花落雨忧</title>

<meta property="og:title" content="「连载四」Gin搭建Blog API&#39;s （三） - 花落雨忧">



    



    
    <meta property="description" content="本节编写文章的逻辑，我们定义一下接口吧！
[&amp;hellip;] 在routers的 v1 版本下，新建article.go文件，写入内容：
[&amp;hellip;] package v1 import ( &amp;#34;github.com/gin-gonic/gin&amp;#34; ) //获取单个文章 func GetArticle(c *gin.Context) { } //获取多个文章 func &amp;hellip;">
    <meta property="og:description" content="本节编写文章的逻辑，我们定义一下接口吧！
[&amp;hellip;] 在routers的 v1 版本下，新建article.go文件，写入内容：
[&amp;hellip;] package v1 import ( &amp;#34;github.com/gin-gonic/gin&amp;#34; ) //获取单个文章 func GetArticle(c *gin.Context) { } //获取多个文章 func &amp;hellip;">
    






<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1">

<link rel="shortcut icon" href="https://www.lican.asia/logo/logo.png" type="image/x-icon" />



<link rel="stylesheet" href="/css/style.min.css" />

<link rel="stylesheet" href="/css/reset.min.css" />




<script src="https://www.lican.asia/js/highlight.min.js"></script>

<script>
hljs.configure({ ignoreUnescapedHTML: true })
hljs.highlightAll();
</script>


<script src="https://www.lican.asia/js/jquery.min.js"></script>




<link href="https://www.lican.asia/css/hugo-code.min.css" rel="stylesheet" />



    <style>
        .post-content img {
            max-width: 400px;
        }
    </style>
</head>

<body id="period" class="home blog">
    <a class="skip-content" href="#main">Press "Enter" to skip to content</a>
    <div id="overflow-container" class="overflow-container">
        <header class="site-header" id="site-header" role="banner">
    <div class="max-width">
        <div id="title-container" class="title-container">
            <div id="site-title" class="site-title"><a href="/">花落雨忧</a></div>
            <p class="tagline">思所及 力所行 方为真.</p>
        </div>
        
        <div id="menu-primary-container" class="menu-primary-container">
            <div class="icon-container">
            </div>
            <div id="menu-primary" class="menu-container menu-primary" role="navigation">
                <nav class="menu">
                    <ul id="menu-primary-items" class="menu-primary-items">
                        
                        
                        <li id="menu-item-0"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/posts" aria-current="page" tabindex="0">首页</a></li>
                        
                        <li id="menu-item-1"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/tech/" aria-current="page" tabindex="1">技术文档</a></li>
                        
                        <li id="menu-item-2"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/article/" aria-current="page" tabindex="2">文章</a></li>
                        
                        <li id="menu-item-3"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/project/" aria-current="page" tabindex="3">项目</a></li>
                        
                        <li id="menu-item-4"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/about/" aria-current="page" tabindex="4">关于</a></li>
                        
                    </ul>
                </nav>
            </div>
        </div>
    </div>
</header>

        <div id="primary-container" class="primary-container">
            <div class="max-width">
                <section id="main" class="main" role="main">
                    <div id="loop-container" class="loop-container">
                        <div
                            class="post type-post status-publish format-standard hentry entry">
                            <article>
                                <div class="post-container">
                                    <div class="post-header">
                                        <h2 class="post-title">
                                            <a href="/posts/posts/go/gin/2018-02-13-api-03/">「连载四」Gin搭建Blog API&#39;s （三）</a>
                                        </h2>
                                        
                                        <div class="post-byline">Published on
                                            <a class="date" href="javascript:;">2018/02/13</a>
                                            
                                            
                                            
                                            By 
                                            <a class="author" href="javascript:;">煎鱼</a></div>
                                            
                                        
                                    </div>
                                    <div class="post-content">
                                        <h2 id="涉及知识点">涉及知识点</h2>
<ul>
<li><a href="https://github.com/gin-gonic/gin">Gin</a>：Golang 的一个微框架，性能极佳。</li>
<li><a href="https://github.com/astaxie/beego/tree/master/validation">beego-validation</a>：本节采用的 beego 的表单验证库，<a href="https://beego.me/docs/mvc/controller/validation.md">中文文档</a>。</li>
<li><a href="https://github.com/jinzhu/gorm">gorm</a>，对开发人员友好的 ORM 框架，<a href="http://gorm.io/docs/">英文文档</a></li>
<li><a href="https://github.com/Unknwon/com">com</a>，一个小而美的工具包。</li>
</ul>
<h2 id="本文目标">本文目标</h2>
<ul>
<li>完成博客的文章类接口定义和编写</li>
</ul>
<h2 id="定义接口">定义接口</h2>
<p>本节编写文章的逻辑，我们定义一下接口吧！</p>
<ul>
<li>获取文章列表：GET(&quot;/articles&quot;)</li>
<li>获取指定文章：POST(&quot;/articles/:id&quot;)</li>
<li>新建文章：POST(&quot;/articles&quot;)</li>
<li>更新指定文章：PUT(&quot;/articles/:id&quot;)</li>
<li>删除指定文章：DELETE(&quot;/articles/:id&quot;)</li>
</ul>
<h2 id="编写路由逻辑">编写路由逻辑</h2>
<p>在<code>routers</code>的 v1 版本下，新建<code>article.go</code>文件，写入内容：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#f92672">package</span> <span style="color:#a6e22e">v1</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">import</span> (
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;github.com/gin-gonic/gin&#34;</span>
</span></span><span style="display:flex;"><span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">//获取单个文章
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">GetArticle</span>(<span style="color:#a6e22e">c</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">Context</span>) {
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">//获取多个文章
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">GetArticles</span>(<span style="color:#a6e22e">c</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">Context</span>) {
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">//新增文章
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">AddArticle</span>(<span style="color:#a6e22e">c</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">Context</span>) {
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">//修改文章
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">EditArticle</span>(<span style="color:#a6e22e">c</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">Context</span>) {
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">//删除文章
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">DeleteArticle</span>(<span style="color:#a6e22e">c</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">Context</span>) {
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>我们打开<code>routers</code>下的<code>router.go</code>文件，修改文件内容为：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#f92672">package</span> <span style="color:#a6e22e">routers</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">import</span> (
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;github.com/gin-gonic/gin&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;github.com/EDDYCJY/go-gin-example/routers/api/v1&#34;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;github.com/EDDYCJY/go-gin-example/pkg/setting&#34;</span>
</span></span><span style="display:flex;"><span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">InitRouter</span>() <span style="color:#f92672">*</span><span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">Engine</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">...</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">apiv1</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">r</span>.<span style="color:#a6e22e">Group</span>(<span style="color:#e6db74">&#34;/api/v1&#34;</span>)
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">...</span>
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">//获取文章列表
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#a6e22e">apiv1</span>.<span style="color:#a6e22e">GET</span>(<span style="color:#e6db74">&#34;/articles&#34;</span>, <span style="color:#a6e22e">v1</span>.<span style="color:#a6e22e">GetArticles</span>)
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">//获取指定文章
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#a6e22e">apiv1</span>.<span style="color:#a6e22e">GET</span>(<span style="color:#e6db74">&#34;/articles/:id&#34;</span>, <span style="color:#a6e22e">v1</span>.<span style="color:#a6e22e">GetArticle</span>)
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">//新建文章
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#a6e22e">apiv1</span>.<span style="color:#a6e22e">POST</span>(<span style="color:#e6db74">&#34;/articles&#34;</span>, <span style="color:#a6e22e">v1</span>.<span style="color:#a6e22e">AddArticle</span>)
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">//更新指定文章
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#a6e22e">apiv1</span>.<span style="color:#a6e22e">PUT</span>(<span style="color:#e6db74">&#34;/articles/:id&#34;</span>, <span style="color:#a6e22e">v1</span>.<span style="color:#a6e22e">EditArticle</span>)
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">//删除指定文章
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>        <span style="color:#a6e22e">apiv1</span>.<span style="color:#a6e22e">DELETE</span>(<span style="color:#e6db74">&#34;/articles/:id&#34;</span>, <span style="color:#a6e22e">v1</span>.<span style="color:#a6e22e">DeleteArticle</span>)
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#a6e22e">r</span>
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>当前目录结构：</p>
<pre tabindex="0"><code>go-gin-example/
├── conf
│   └── app.ini
├── main.go
├── middleware
├── models
│   ├── models.go
│   └── tag.go
├── pkg
│   ├── e
│   │   ├── code.go
│   │   └── msg.go
│   ├── setting
│   │   └── setting.go
│   └── util
│       └── pagination.go
├── routers
│   ├── api
│   │   └── v1
│   │       ├── article.go
│   │       └── tag.go
│   └── router.go
├── runtime
</code></pre><p>在基础的路由规则配置结束后，我们<strong>开始编写我们的接口</strong>吧！</p>
<hr>
<p>##编写 models 逻辑
创建<code>models</code>目录下的<code>article.go</code>，写入文件内容：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#f92672">package</span> <span style="color:#a6e22e">models</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">import</span> (
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;github.com/jinzhu/gorm&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;time&#34;</span>
</span></span><span style="display:flex;"><span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">type</span> <span style="color:#a6e22e">Article</span> <span style="color:#66d9ef">struct</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">Model</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">TagID</span> <span style="color:#66d9ef">int</span> <span style="color:#e6db74">`json:&#34;tag_id&#34; gorm:&#34;index&#34;`</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">Tag</span>   <span style="color:#a6e22e">Tag</span> <span style="color:#e6db74">`json:&#34;tag&#34;`</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">Title</span> <span style="color:#66d9ef">string</span> <span style="color:#e6db74">`json:&#34;title&#34;`</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">Desc</span> <span style="color:#66d9ef">string</span> <span style="color:#e6db74">`json:&#34;desc&#34;`</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">Content</span> <span style="color:#66d9ef">string</span> <span style="color:#e6db74">`json:&#34;content&#34;`</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">CreatedBy</span> <span style="color:#66d9ef">string</span> <span style="color:#e6db74">`json:&#34;created_by&#34;`</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">ModifiedBy</span> <span style="color:#66d9ef">string</span> <span style="color:#e6db74">`json:&#34;modified_by&#34;`</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">State</span> <span style="color:#66d9ef">int</span> <span style="color:#e6db74">`json:&#34;state&#34;`</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> (<span style="color:#a6e22e">article</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">Article</span>) <span style="color:#a6e22e">BeforeCreate</span>(<span style="color:#a6e22e">scope</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gorm</span>.<span style="color:#a6e22e">Scope</span>) <span style="color:#66d9ef">error</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">scope</span>.<span style="color:#a6e22e">SetColumn</span>(<span style="color:#e6db74">&#34;CreatedOn&#34;</span>, <span style="color:#a6e22e">time</span>.<span style="color:#a6e22e">Now</span>().<span style="color:#a6e22e">Unix</span>())
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">nil</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> (<span style="color:#a6e22e">article</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">Article</span>) <span style="color:#a6e22e">BeforeUpdate</span>(<span style="color:#a6e22e">scope</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gorm</span>.<span style="color:#a6e22e">Scope</span>) <span style="color:#66d9ef">error</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">scope</span>.<span style="color:#a6e22e">SetColumn</span>(<span style="color:#e6db74">&#34;ModifiedOn&#34;</span>, <span style="color:#a6e22e">time</span>.<span style="color:#a6e22e">Now</span>().<span style="color:#a6e22e">Unix</span>())
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">nil</span>
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>我们创建了一个<code>Article struct {}</code>，与<code>Tag</code>不同的是，<code>Article</code>多了几项，如下：</p>
<ol>
<li><code>gorm:index</code>，用于声明这个字段为索引，如果你使用了自动迁移功能则会有所影响，在不使用则无影响</li>
<li><code>Tag</code>字段，实际是一个嵌套的<code>struct</code>，它利用<code>TagID</code>与<code>Tag</code>模型相互关联，在执行查询的时候，能够达到<code>Article</code>、<code>Tag</code>关联查询的功能</li>
<li><code>time.Now().Unix()</code> 返回当前的时间戳</li>
</ol>
<p>接下来，请确保已对上一章节的内容通读且了解，由于逻辑偏差不会太远，我们本节直接编写这五个接口</p>
<hr>
<p>打开<code>models</code>目录下的<code>article.go</code>，修改文件内容：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#f92672">package</span> <span style="color:#a6e22e">models</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">import</span> (
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;time&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;github.com/jinzhu/gorm&#34;</span>
</span></span><span style="display:flex;"><span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">type</span> <span style="color:#a6e22e">Article</span> <span style="color:#66d9ef">struct</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">Model</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">TagID</span> <span style="color:#66d9ef">int</span> <span style="color:#e6db74">`json:&#34;tag_id&#34; gorm:&#34;index&#34;`</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">Tag</span>   <span style="color:#a6e22e">Tag</span> <span style="color:#e6db74">`json:&#34;tag&#34;`</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">Title</span> <span style="color:#66d9ef">string</span> <span style="color:#e6db74">`json:&#34;title&#34;`</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">Desc</span> <span style="color:#66d9ef">string</span> <span style="color:#e6db74">`json:&#34;desc&#34;`</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">Content</span> <span style="color:#66d9ef">string</span> <span style="color:#e6db74">`json:&#34;content&#34;`</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">CreatedBy</span> <span style="color:#66d9ef">string</span> <span style="color:#e6db74">`json:&#34;created_by&#34;`</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">ModifiedBy</span> <span style="color:#66d9ef">string</span> <span style="color:#e6db74">`json:&#34;modified_by&#34;`</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">State</span> <span style="color:#66d9ef">int</span> <span style="color:#e6db74">`json:&#34;state&#34;`</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">ExistArticleByID</span>(<span style="color:#a6e22e">id</span> <span style="color:#66d9ef">int</span>) <span style="color:#66d9ef">bool</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">var</span> <span style="color:#a6e22e">article</span> <span style="color:#a6e22e">Article</span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Select</span>(<span style="color:#e6db74">&#34;id&#34;</span>).<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;id = ?&#34;</span>, <span style="color:#a6e22e">id</span>).<span style="color:#a6e22e">First</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">article</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">article</span>.<span style="color:#a6e22e">ID</span> &gt; <span style="color:#ae81ff">0</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">true</span>
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">false</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">GetArticleTotal</span>(<span style="color:#a6e22e">maps</span> <span style="color:#66d9ef">interface</span> {}) (<span style="color:#a6e22e">count</span> <span style="color:#66d9ef">int</span>){
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">Article</span>{}).<span style="color:#a6e22e">Where</span>(<span style="color:#a6e22e">maps</span>).<span style="color:#a6e22e">Count</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">count</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">GetArticles</span>(<span style="color:#a6e22e">pageNum</span> <span style="color:#66d9ef">int</span>, <span style="color:#a6e22e">pageSize</span> <span style="color:#66d9ef">int</span>, <span style="color:#a6e22e">maps</span> <span style="color:#66d9ef">interface</span> {}) (<span style="color:#a6e22e">articles</span> []<span style="color:#a6e22e">Article</span>) {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Preload</span>(<span style="color:#e6db74">&#34;Tag&#34;</span>).<span style="color:#a6e22e">Where</span>(<span style="color:#a6e22e">maps</span>).<span style="color:#a6e22e">Offset</span>(<span style="color:#a6e22e">pageNum</span>).<span style="color:#a6e22e">Limit</span>(<span style="color:#a6e22e">pageSize</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">articles</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">GetArticle</span>(<span style="color:#a6e22e">id</span> <span style="color:#66d9ef">int</span>) (<span style="color:#a6e22e">article</span> <span style="color:#a6e22e">Article</span>) {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;id = ?&#34;</span>, <span style="color:#a6e22e">id</span>).<span style="color:#a6e22e">First</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">article</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">article</span>).<span style="color:#a6e22e">Related</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">article</span>.<span style="color:#a6e22e">Tag</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">EditArticle</span>(<span style="color:#a6e22e">id</span> <span style="color:#66d9ef">int</span>, <span style="color:#a6e22e">data</span> <span style="color:#66d9ef">interface</span> {}) <span style="color:#66d9ef">bool</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">Article</span>{}).<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;id = ?&#34;</span>, <span style="color:#a6e22e">id</span>).<span style="color:#a6e22e">Updates</span>(<span style="color:#a6e22e">data</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">true</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">AddArticle</span>(<span style="color:#a6e22e">data</span> <span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">interface</span> {}) <span style="color:#66d9ef">bool</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Create</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">Article</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">TagID</span> : <span style="color:#a6e22e">data</span>[<span style="color:#e6db74">&#34;tag_id&#34;</span>].(<span style="color:#66d9ef">int</span>),
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">Title</span> : <span style="color:#a6e22e">data</span>[<span style="color:#e6db74">&#34;title&#34;</span>].(<span style="color:#66d9ef">string</span>),
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">Desc</span> : <span style="color:#a6e22e">data</span>[<span style="color:#e6db74">&#34;desc&#34;</span>].(<span style="color:#66d9ef">string</span>),
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">Content</span> : <span style="color:#a6e22e">data</span>[<span style="color:#e6db74">&#34;content&#34;</span>].(<span style="color:#66d9ef">string</span>),
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">CreatedBy</span> : <span style="color:#a6e22e">data</span>[<span style="color:#e6db74">&#34;created_by&#34;</span>].(<span style="color:#66d9ef">string</span>),
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">State</span> : <span style="color:#a6e22e">data</span>[<span style="color:#e6db74">&#34;state&#34;</span>].(<span style="color:#66d9ef">int</span>),
</span></span><span style="display:flex;"><span>    })
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">true</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">DeleteArticle</span>(<span style="color:#a6e22e">id</span> <span style="color:#66d9ef">int</span>) <span style="color:#66d9ef">bool</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;id = ?&#34;</span>, <span style="color:#a6e22e">id</span>).<span style="color:#a6e22e">Delete</span>(<span style="color:#a6e22e">Article</span>{})
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">true</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> (<span style="color:#a6e22e">article</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">Article</span>) <span style="color:#a6e22e">BeforeCreate</span>(<span style="color:#a6e22e">scope</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gorm</span>.<span style="color:#a6e22e">Scope</span>) <span style="color:#66d9ef">error</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">scope</span>.<span style="color:#a6e22e">SetColumn</span>(<span style="color:#e6db74">&#34;CreatedOn&#34;</span>, <span style="color:#a6e22e">time</span>.<span style="color:#a6e22e">Now</span>().<span style="color:#a6e22e">Unix</span>())
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">nil</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> (<span style="color:#a6e22e">article</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">Article</span>) <span style="color:#a6e22e">BeforeUpdate</span>(<span style="color:#a6e22e">scope</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gorm</span>.<span style="color:#a6e22e">Scope</span>) <span style="color:#66d9ef">error</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">scope</span>.<span style="color:#a6e22e">SetColumn</span>(<span style="color:#e6db74">&#34;ModifiedOn&#34;</span>, <span style="color:#a6e22e">time</span>.<span style="color:#a6e22e">Now</span>().<span style="color:#a6e22e">Unix</span>())
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">nil</span>
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>在这里，我们拿出三点不同来讲，如下：</p>
<p><strong>1、 我们的<code>Article</code>是如何关联到<code>Tag</code>？</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">GetArticle</span>(<span style="color:#a6e22e">id</span> <span style="color:#66d9ef">int</span>) (<span style="color:#a6e22e">article</span> <span style="color:#a6e22e">Article</span>) {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Where</span>(<span style="color:#e6db74">&#34;id = ?&#34;</span>, <span style="color:#a6e22e">id</span>).<span style="color:#a6e22e">First</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">article</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Model</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">article</span>).<span style="color:#a6e22e">Related</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">article</span>.<span style="color:#a6e22e">Tag</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span>
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>能够达到关联，首先是<code>gorm</code>本身做了大量的约定俗成</p>
<ul>
<li><code>Article</code>有一个结构体成员是<code>TagID</code>，就是外键。<code>gorm</code>会通过类名+ID 的方式去找到这两个类之间的关联关系</li>
<li><code>Article</code>有一个结构体成员是<code>Tag</code>，就是我们嵌套在<code>Article</code>里的<code>Tag</code>结构体，我们可以通过<code>Related</code>进行关联查询</li>
</ul>
<p><strong>2、 <code>Preload</code>是什么东西，为什么查询可以得出每一项的关联<code>Tag</code>？</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">GetArticles</span>(<span style="color:#a6e22e">pageNum</span> <span style="color:#66d9ef">int</span>, <span style="color:#a6e22e">pageSize</span> <span style="color:#66d9ef">int</span>, <span style="color:#a6e22e">maps</span> <span style="color:#66d9ef">interface</span> {}) (<span style="color:#a6e22e">articles</span> []<span style="color:#a6e22e">Article</span>) {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">db</span>.<span style="color:#a6e22e">Preload</span>(<span style="color:#e6db74">&#34;Tag&#34;</span>).<span style="color:#a6e22e">Where</span>(<span style="color:#a6e22e">maps</span>).<span style="color:#a6e22e">Offset</span>(<span style="color:#a6e22e">pageNum</span>).<span style="color:#a6e22e">Limit</span>(<span style="color:#a6e22e">pageSize</span>).<span style="color:#a6e22e">Find</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">articles</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span>
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p><code>Preload</code>就是一个预加载器，它会执行两条 SQL，分别是<code>SELECT * FROM blog_articles;</code>和<code>SELECT * FROM blog_tag WHERE id IN (1,2,3,4);</code>，那么在查询出结构后，<code>gorm</code>内部处理对应的映射逻辑，将其填充到<code>Article</code>的<code>Tag</code>中，会特别方便，并且避免了循环查询</p>
<p>那么有没有别的办法呢，大致是两种</p>
<ul>
<li><code>gorm</code>的<code>Join</code></li>
<li>循环<code>Related</code></li>
</ul>
<p>综合之下，还是<code>Preload</code>更好，如果你有更优的方案，欢迎说一下 :)</p>
<p><strong>3、 <code>v.(I)</code> 是什么？</strong></p>
<p><code>v</code>表示一个接口值，<code>I</code>表示接口类型。这个实际就是 Golang 中的<strong>类型断言</strong>，用于判断一个接口值的实际类型是否为某个类型，或一个非接口值的类型是否实现了某个接口类型</p>
<hr>
<p>打开<code>routers</code>目录下 v1 版本的<code>article.go</code>文件，修改文件内容：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#f92672">package</span> <span style="color:#a6e22e">v1</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">import</span> (
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;net/http&#34;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;log&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;github.com/gin-gonic/gin&#34;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;github.com/astaxie/beego/validation&#34;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;github.com/unknwon/com&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;github.com/EDDYCJY/go-gin-example/models&#34;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;github.com/EDDYCJY/go-gin-example/pkg/e&#34;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;github.com/EDDYCJY/go-gin-example/pkg/setting&#34;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;github.com/EDDYCJY/go-gin-example/pkg/util&#34;</span>
</span></span><span style="display:flex;"><span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">//获取单个文章
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">GetArticle</span>(<span style="color:#a6e22e">c</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">Context</span>) {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">id</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">com</span>.<span style="color:#a6e22e">StrTo</span>(<span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">Param</span>(<span style="color:#e6db74">&#34;id&#34;</span>)).<span style="color:#a6e22e">MustInt</span>()
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">valid</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">validation</span>.<span style="color:#a6e22e">Validation</span>{}
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">Min</span>(<span style="color:#a6e22e">id</span>, <span style="color:#ae81ff">1</span>, <span style="color:#e6db74">&#34;id&#34;</span>).<span style="color:#a6e22e">Message</span>(<span style="color:#e6db74">&#34;ID必须大于0&#34;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">code</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">INVALID_PARAMS</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">var</span> <span style="color:#a6e22e">data</span> <span style="color:#66d9ef">interface</span> {}
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span> ! <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">HasErrors</span>() {
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">models</span>.<span style="color:#a6e22e">ExistArticleByID</span>(<span style="color:#a6e22e">id</span>) {
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">data</span> = <span style="color:#a6e22e">models</span>.<span style="color:#a6e22e">GetArticle</span>(<span style="color:#a6e22e">id</span>)
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">code</span> = <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">SUCCESS</span>
</span></span><span style="display:flex;"><span>        } <span style="color:#66d9ef">else</span> {
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">code</span> = <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">ERROR_NOT_EXIST_ARTICLE</span>
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    } <span style="color:#66d9ef">else</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">for</span> <span style="color:#a6e22e">_</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#66d9ef">range</span> <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">Errors</span> {
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">log</span>.<span style="color:#a6e22e">Printf</span>(<span style="color:#e6db74">&#34;err.key: %s, err.message: %s&#34;</span>, <span style="color:#a6e22e">err</span>.<span style="color:#a6e22e">Key</span>, <span style="color:#a6e22e">err</span>.<span style="color:#a6e22e">Message</span>)
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">JSON</span>(<span style="color:#a6e22e">http</span>.<span style="color:#a6e22e">StatusOK</span>, <span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">H</span>{
</span></span><span style="display:flex;"><span>        <span style="color:#e6db74">&#34;code&#34;</span> : <span style="color:#a6e22e">code</span>,
</span></span><span style="display:flex;"><span>        <span style="color:#e6db74">&#34;msg&#34;</span> : <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">GetMsg</span>(<span style="color:#a6e22e">code</span>),
</span></span><span style="display:flex;"><span>        <span style="color:#e6db74">&#34;data&#34;</span> : <span style="color:#a6e22e">data</span>,
</span></span><span style="display:flex;"><span>    })
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">//获取多个文章
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">GetArticles</span>(<span style="color:#a6e22e">c</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">Context</span>) {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">data</span> <span style="color:#f92672">:=</span> make(<span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">interface</span>{})
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">maps</span> <span style="color:#f92672">:=</span> make(<span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">interface</span>{})
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">valid</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">validation</span>.<span style="color:#a6e22e">Validation</span>{}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">var</span> <span style="color:#a6e22e">state</span> <span style="color:#66d9ef">int</span> = <span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">arg</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">Query</span>(<span style="color:#e6db74">&#34;state&#34;</span>); <span style="color:#a6e22e">arg</span> <span style="color:#f92672">!=</span> <span style="color:#e6db74">&#34;&#34;</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">state</span> = <span style="color:#a6e22e">com</span>.<span style="color:#a6e22e">StrTo</span>(<span style="color:#a6e22e">arg</span>).<span style="color:#a6e22e">MustInt</span>()
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">maps</span>[<span style="color:#e6db74">&#34;state&#34;</span>] = <span style="color:#a6e22e">state</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">Range</span>(<span style="color:#a6e22e">state</span>, <span style="color:#ae81ff">0</span>, <span style="color:#ae81ff">1</span>, <span style="color:#e6db74">&#34;state&#34;</span>).<span style="color:#a6e22e">Message</span>(<span style="color:#e6db74">&#34;状态只允许0或1&#34;</span>)
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">var</span> <span style="color:#a6e22e">tagId</span> <span style="color:#66d9ef">int</span> = <span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">arg</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">Query</span>(<span style="color:#e6db74">&#34;tag_id&#34;</span>); <span style="color:#a6e22e">arg</span> <span style="color:#f92672">!=</span> <span style="color:#e6db74">&#34;&#34;</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">tagId</span> = <span style="color:#a6e22e">com</span>.<span style="color:#a6e22e">StrTo</span>(<span style="color:#a6e22e">arg</span>).<span style="color:#a6e22e">MustInt</span>()
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">maps</span>[<span style="color:#e6db74">&#34;tag_id&#34;</span>] = <span style="color:#a6e22e">tagId</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">Min</span>(<span style="color:#a6e22e">tagId</span>, <span style="color:#ae81ff">1</span>, <span style="color:#e6db74">&#34;tag_id&#34;</span>).<span style="color:#a6e22e">Message</span>(<span style="color:#e6db74">&#34;标签ID必须大于0&#34;</span>)
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">code</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">INVALID_PARAMS</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span> ! <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">HasErrors</span>() {
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">code</span> = <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">SUCCESS</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">data</span>[<span style="color:#e6db74">&#34;lists&#34;</span>] = <span style="color:#a6e22e">models</span>.<span style="color:#a6e22e">GetArticles</span>(<span style="color:#a6e22e">util</span>.<span style="color:#a6e22e">GetPage</span>(<span style="color:#a6e22e">c</span>), <span style="color:#a6e22e">setting</span>.<span style="color:#a6e22e">PageSize</span>, <span style="color:#a6e22e">maps</span>)
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">data</span>[<span style="color:#e6db74">&#34;total&#34;</span>] = <span style="color:#a6e22e">models</span>.<span style="color:#a6e22e">GetArticleTotal</span>(<span style="color:#a6e22e">maps</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    } <span style="color:#66d9ef">else</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">for</span> <span style="color:#a6e22e">_</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#66d9ef">range</span> <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">Errors</span> {
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">log</span>.<span style="color:#a6e22e">Printf</span>(<span style="color:#e6db74">&#34;err.key: %s, err.message: %s&#34;</span>, <span style="color:#a6e22e">err</span>.<span style="color:#a6e22e">Key</span>, <span style="color:#a6e22e">err</span>.<span style="color:#a6e22e">Message</span>)
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">JSON</span>(<span style="color:#a6e22e">http</span>.<span style="color:#a6e22e">StatusOK</span>, <span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">H</span>{
</span></span><span style="display:flex;"><span>        <span style="color:#e6db74">&#34;code&#34;</span> : <span style="color:#a6e22e">code</span>,
</span></span><span style="display:flex;"><span>        <span style="color:#e6db74">&#34;msg&#34;</span> : <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">GetMsg</span>(<span style="color:#a6e22e">code</span>),
</span></span><span style="display:flex;"><span>        <span style="color:#e6db74">&#34;data&#34;</span> : <span style="color:#a6e22e">data</span>,
</span></span><span style="display:flex;"><span>    })
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">//新增文章
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">AddArticle</span>(<span style="color:#a6e22e">c</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">Context</span>) {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">tagId</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">com</span>.<span style="color:#a6e22e">StrTo</span>(<span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">Query</span>(<span style="color:#e6db74">&#34;tag_id&#34;</span>)).<span style="color:#a6e22e">MustInt</span>()
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">title</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">Query</span>(<span style="color:#e6db74">&#34;title&#34;</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">desc</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">Query</span>(<span style="color:#e6db74">&#34;desc&#34;</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">content</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">Query</span>(<span style="color:#e6db74">&#34;content&#34;</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">createdBy</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">Query</span>(<span style="color:#e6db74">&#34;created_by&#34;</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">state</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">com</span>.<span style="color:#a6e22e">StrTo</span>(<span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">DefaultQuery</span>(<span style="color:#e6db74">&#34;state&#34;</span>, <span style="color:#e6db74">&#34;0&#34;</span>)).<span style="color:#a6e22e">MustInt</span>()
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">valid</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">validation</span>.<span style="color:#a6e22e">Validation</span>{}
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">Min</span>(<span style="color:#a6e22e">tagId</span>, <span style="color:#ae81ff">1</span>, <span style="color:#e6db74">&#34;tag_id&#34;</span>).<span style="color:#a6e22e">Message</span>(<span style="color:#e6db74">&#34;标签ID必须大于0&#34;</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">Required</span>(<span style="color:#a6e22e">title</span>, <span style="color:#e6db74">&#34;title&#34;</span>).<span style="color:#a6e22e">Message</span>(<span style="color:#e6db74">&#34;标题不能为空&#34;</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">Required</span>(<span style="color:#a6e22e">desc</span>, <span style="color:#e6db74">&#34;desc&#34;</span>).<span style="color:#a6e22e">Message</span>(<span style="color:#e6db74">&#34;简述不能为空&#34;</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">Required</span>(<span style="color:#a6e22e">content</span>, <span style="color:#e6db74">&#34;content&#34;</span>).<span style="color:#a6e22e">Message</span>(<span style="color:#e6db74">&#34;内容不能为空&#34;</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">Required</span>(<span style="color:#a6e22e">createdBy</span>, <span style="color:#e6db74">&#34;created_by&#34;</span>).<span style="color:#a6e22e">Message</span>(<span style="color:#e6db74">&#34;创建人不能为空&#34;</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">Range</span>(<span style="color:#a6e22e">state</span>, <span style="color:#ae81ff">0</span>, <span style="color:#ae81ff">1</span>, <span style="color:#e6db74">&#34;state&#34;</span>).<span style="color:#a6e22e">Message</span>(<span style="color:#e6db74">&#34;状态只允许0或1&#34;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">code</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">INVALID_PARAMS</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span> ! <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">HasErrors</span>() {
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">models</span>.<span style="color:#a6e22e">ExistTagByID</span>(<span style="color:#a6e22e">tagId</span>) {
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">data</span> <span style="color:#f92672">:=</span> make(<span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">interface</span> {})
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">data</span>[<span style="color:#e6db74">&#34;tag_id&#34;</span>] = <span style="color:#a6e22e">tagId</span>
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">data</span>[<span style="color:#e6db74">&#34;title&#34;</span>] = <span style="color:#a6e22e">title</span>
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">data</span>[<span style="color:#e6db74">&#34;desc&#34;</span>] = <span style="color:#a6e22e">desc</span>
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">data</span>[<span style="color:#e6db74">&#34;content&#34;</span>] = <span style="color:#a6e22e">content</span>
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">data</span>[<span style="color:#e6db74">&#34;created_by&#34;</span>] = <span style="color:#a6e22e">createdBy</span>
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">data</span>[<span style="color:#e6db74">&#34;state&#34;</span>] = <span style="color:#a6e22e">state</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">models</span>.<span style="color:#a6e22e">AddArticle</span>(<span style="color:#a6e22e">data</span>)
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">code</span> = <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">SUCCESS</span>
</span></span><span style="display:flex;"><span>        } <span style="color:#66d9ef">else</span> {
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">code</span> = <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">ERROR_NOT_EXIST_TAG</span>
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    } <span style="color:#66d9ef">else</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">for</span> <span style="color:#a6e22e">_</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#66d9ef">range</span> <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">Errors</span> {
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">log</span>.<span style="color:#a6e22e">Printf</span>(<span style="color:#e6db74">&#34;err.key: %s, err.message: %s&#34;</span>, <span style="color:#a6e22e">err</span>.<span style="color:#a6e22e">Key</span>, <span style="color:#a6e22e">err</span>.<span style="color:#a6e22e">Message</span>)
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">JSON</span>(<span style="color:#a6e22e">http</span>.<span style="color:#a6e22e">StatusOK</span>, <span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">H</span>{
</span></span><span style="display:flex;"><span>        <span style="color:#e6db74">&#34;code&#34;</span> : <span style="color:#a6e22e">code</span>,
</span></span><span style="display:flex;"><span>        <span style="color:#e6db74">&#34;msg&#34;</span> : <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">GetMsg</span>(<span style="color:#a6e22e">code</span>),
</span></span><span style="display:flex;"><span>        <span style="color:#e6db74">&#34;data&#34;</span> : make(<span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">interface</span>{}),
</span></span><span style="display:flex;"><span>    })
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">//修改文章
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">EditArticle</span>(<span style="color:#a6e22e">c</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">Context</span>) {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">valid</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">validation</span>.<span style="color:#a6e22e">Validation</span>{}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">id</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">com</span>.<span style="color:#a6e22e">StrTo</span>(<span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">Param</span>(<span style="color:#e6db74">&#34;id&#34;</span>)).<span style="color:#a6e22e">MustInt</span>()
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">tagId</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">com</span>.<span style="color:#a6e22e">StrTo</span>(<span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">Query</span>(<span style="color:#e6db74">&#34;tag_id&#34;</span>)).<span style="color:#a6e22e">MustInt</span>()
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">title</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">Query</span>(<span style="color:#e6db74">&#34;title&#34;</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">desc</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">Query</span>(<span style="color:#e6db74">&#34;desc&#34;</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">content</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">Query</span>(<span style="color:#e6db74">&#34;content&#34;</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">modifiedBy</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">Query</span>(<span style="color:#e6db74">&#34;modified_by&#34;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">var</span> <span style="color:#a6e22e">state</span> <span style="color:#66d9ef">int</span> = <span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">arg</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">Query</span>(<span style="color:#e6db74">&#34;state&#34;</span>); <span style="color:#a6e22e">arg</span> <span style="color:#f92672">!=</span> <span style="color:#e6db74">&#34;&#34;</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">state</span> = <span style="color:#a6e22e">com</span>.<span style="color:#a6e22e">StrTo</span>(<span style="color:#a6e22e">arg</span>).<span style="color:#a6e22e">MustInt</span>()
</span></span><span style="display:flex;"><span>        <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">Range</span>(<span style="color:#a6e22e">state</span>, <span style="color:#ae81ff">0</span>, <span style="color:#ae81ff">1</span>, <span style="color:#e6db74">&#34;state&#34;</span>).<span style="color:#a6e22e">Message</span>(<span style="color:#e6db74">&#34;状态只允许0或1&#34;</span>)
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">Min</span>(<span style="color:#a6e22e">id</span>, <span style="color:#ae81ff">1</span>, <span style="color:#e6db74">&#34;id&#34;</span>).<span style="color:#a6e22e">Message</span>(<span style="color:#e6db74">&#34;ID必须大于0&#34;</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">MaxSize</span>(<span style="color:#a6e22e">title</span>, <span style="color:#ae81ff">100</span>, <span style="color:#e6db74">&#34;title&#34;</span>).<span style="color:#a6e22e">Message</span>(<span style="color:#e6db74">&#34;标题最长为100字符&#34;</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">MaxSize</span>(<span style="color:#a6e22e">desc</span>, <span style="color:#ae81ff">255</span>, <span style="color:#e6db74">&#34;desc&#34;</span>).<span style="color:#a6e22e">Message</span>(<span style="color:#e6db74">&#34;简述最长为255字符&#34;</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">MaxSize</span>(<span style="color:#a6e22e">content</span>, <span style="color:#ae81ff">65535</span>, <span style="color:#e6db74">&#34;content&#34;</span>).<span style="color:#a6e22e">Message</span>(<span style="color:#e6db74">&#34;内容最长为65535字符&#34;</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">Required</span>(<span style="color:#a6e22e">modifiedBy</span>, <span style="color:#e6db74">&#34;modified_by&#34;</span>).<span style="color:#a6e22e">Message</span>(<span style="color:#e6db74">&#34;修改人不能为空&#34;</span>)
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">MaxSize</span>(<span style="color:#a6e22e">modifiedBy</span>, <span style="color:#ae81ff">100</span>, <span style="color:#e6db74">&#34;modified_by&#34;</span>).<span style="color:#a6e22e">Message</span>(<span style="color:#e6db74">&#34;修改人最长为100字符&#34;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">code</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">INVALID_PARAMS</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span> ! <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">HasErrors</span>() {
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">models</span>.<span style="color:#a6e22e">ExistArticleByID</span>(<span style="color:#a6e22e">id</span>) {
</span></span><span style="display:flex;"><span>            <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">models</span>.<span style="color:#a6e22e">ExistTagByID</span>(<span style="color:#a6e22e">tagId</span>) {
</span></span><span style="display:flex;"><span>                <span style="color:#a6e22e">data</span> <span style="color:#f92672">:=</span> make(<span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">interface</span> {})
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">tagId</span> &gt; <span style="color:#ae81ff">0</span> {
</span></span><span style="display:flex;"><span>                    <span style="color:#a6e22e">data</span>[<span style="color:#e6db74">&#34;tag_id&#34;</span>] = <span style="color:#a6e22e">tagId</span>
</span></span><span style="display:flex;"><span>                }
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">title</span> <span style="color:#f92672">!=</span> <span style="color:#e6db74">&#34;&#34;</span> {
</span></span><span style="display:flex;"><span>                    <span style="color:#a6e22e">data</span>[<span style="color:#e6db74">&#34;title&#34;</span>] = <span style="color:#a6e22e">title</span>
</span></span><span style="display:flex;"><span>                }
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">desc</span> <span style="color:#f92672">!=</span> <span style="color:#e6db74">&#34;&#34;</span> {
</span></span><span style="display:flex;"><span>                    <span style="color:#a6e22e">data</span>[<span style="color:#e6db74">&#34;desc&#34;</span>] = <span style="color:#a6e22e">desc</span>
</span></span><span style="display:flex;"><span>                }
</span></span><span style="display:flex;"><span>                <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">content</span> <span style="color:#f92672">!=</span> <span style="color:#e6db74">&#34;&#34;</span> {
</span></span><span style="display:flex;"><span>                    <span style="color:#a6e22e">data</span>[<span style="color:#e6db74">&#34;content&#34;</span>] = <span style="color:#a6e22e">content</span>
</span></span><span style="display:flex;"><span>                }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>                <span style="color:#a6e22e">data</span>[<span style="color:#e6db74">&#34;modified_by&#34;</span>] = <span style="color:#a6e22e">modifiedBy</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>                <span style="color:#a6e22e">models</span>.<span style="color:#a6e22e">EditArticle</span>(<span style="color:#a6e22e">id</span>, <span style="color:#a6e22e">data</span>)
</span></span><span style="display:flex;"><span>                <span style="color:#a6e22e">code</span> = <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">SUCCESS</span>
</span></span><span style="display:flex;"><span>            } <span style="color:#66d9ef">else</span> {
</span></span><span style="display:flex;"><span>                <span style="color:#a6e22e">code</span> = <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">ERROR_NOT_EXIST_TAG</span>
</span></span><span style="display:flex;"><span>            }
</span></span><span style="display:flex;"><span>        } <span style="color:#66d9ef">else</span> {
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">code</span> = <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">ERROR_NOT_EXIST_ARTICLE</span>
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    } <span style="color:#66d9ef">else</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">for</span> <span style="color:#a6e22e">_</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#66d9ef">range</span> <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">Errors</span> {
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">log</span>.<span style="color:#a6e22e">Printf</span>(<span style="color:#e6db74">&#34;err.key: %s, err.message: %s&#34;</span>, <span style="color:#a6e22e">err</span>.<span style="color:#a6e22e">Key</span>, <span style="color:#a6e22e">err</span>.<span style="color:#a6e22e">Message</span>)
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">JSON</span>(<span style="color:#a6e22e">http</span>.<span style="color:#a6e22e">StatusOK</span>, <span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">H</span>{
</span></span><span style="display:flex;"><span>        <span style="color:#e6db74">&#34;code&#34;</span> : <span style="color:#a6e22e">code</span>,
</span></span><span style="display:flex;"><span>        <span style="color:#e6db74">&#34;msg&#34;</span> : <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">GetMsg</span>(<span style="color:#a6e22e">code</span>),
</span></span><span style="display:flex;"><span>        <span style="color:#e6db74">&#34;data&#34;</span> : make(<span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">string</span>),
</span></span><span style="display:flex;"><span>    })
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">//删除文章
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">DeleteArticle</span>(<span style="color:#a6e22e">c</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">Context</span>) {
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">id</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">com</span>.<span style="color:#a6e22e">StrTo</span>(<span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">Param</span>(<span style="color:#e6db74">&#34;id&#34;</span>)).<span style="color:#a6e22e">MustInt</span>()
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">valid</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">validation</span>.<span style="color:#a6e22e">Validation</span>{}
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">Min</span>(<span style="color:#a6e22e">id</span>, <span style="color:#ae81ff">1</span>, <span style="color:#e6db74">&#34;id&#34;</span>).<span style="color:#a6e22e">Message</span>(<span style="color:#e6db74">&#34;ID必须大于0&#34;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">code</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">INVALID_PARAMS</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span> ! <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">HasErrors</span>() {
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">models</span>.<span style="color:#a6e22e">ExistArticleByID</span>(<span style="color:#a6e22e">id</span>) {
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">models</span>.<span style="color:#a6e22e">DeleteArticle</span>(<span style="color:#a6e22e">id</span>)
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">code</span> = <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">SUCCESS</span>
</span></span><span style="display:flex;"><span>        } <span style="color:#66d9ef">else</span> {
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">code</span> = <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">ERROR_NOT_EXIST_ARTICLE</span>
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    } <span style="color:#66d9ef">else</span> {
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">for</span> <span style="color:#a6e22e">_</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#66d9ef">range</span> <span style="color:#a6e22e">valid</span>.<span style="color:#a6e22e">Errors</span> {
</span></span><span style="display:flex;"><span>            <span style="color:#a6e22e">log</span>.<span style="color:#a6e22e">Printf</span>(<span style="color:#e6db74">&#34;err.key: %s, err.message: %s&#34;</span>, <span style="color:#a6e22e">err</span>.<span style="color:#a6e22e">Key</span>, <span style="color:#a6e22e">err</span>.<span style="color:#a6e22e">Message</span>)
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">JSON</span>(<span style="color:#a6e22e">http</span>.<span style="color:#a6e22e">StatusOK</span>, <span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">H</span>{
</span></span><span style="display:flex;"><span>        <span style="color:#e6db74">&#34;code&#34;</span> : <span style="color:#a6e22e">code</span>,
</span></span><span style="display:flex;"><span>        <span style="color:#e6db74">&#34;msg&#34;</span> : <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">GetMsg</span>(<span style="color:#a6e22e">code</span>),
</span></span><span style="display:flex;"><span>        <span style="color:#e6db74">&#34;data&#34;</span> : make(<span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">string</span>),
</span></span><span style="display:flex;"><span>    })
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>当前目录结构：</p>
<pre tabindex="0"><code>go-gin-example/
├── conf
│   └── app.ini
├── main.go
├── middleware
├── models
│   ├── article.go
│   ├── models.go
│   └── tag.go
├── pkg
│   ├── e
│   │   ├── code.go
│   │   └── msg.go
│   ├── setting
│   │   └── setting.go
│   └── util
│       └── pagination.go
├── routers
│   ├── api
│   │   └── v1
│   │       ├── article.go
│   │       └── tag.go
│   └── router.go
├── runtime
</code></pre><h2 id="验证功能">验证功能</h2>
<p>我们重启服务，执行<code>go run main.go</code>，检查控制台输出结果</p>
<pre tabindex="0"><code>$ go run main.go
[GIN-debug] [WARNING] Running in &#34;debug&#34; mode. Switch to &#34;release&#34; mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET    /api/v1/tags              --&gt; gin-blog/routers/api/v1.GetTags (3 handlers)
[GIN-debug] POST   /api/v1/tags              --&gt; gin-blog/routers/api/v1.AddTag (3 handlers)
[GIN-debug] PUT    /api/v1/tags/:id          --&gt; gin-blog/routers/api/v1.EditTag (3 handlers)
[GIN-debug] DELETE /api/v1/tags/:id          --&gt; gin-blog/routers/api/v1.DeleteTag (3 handlers)
[GIN-debug] GET    /api/v1/articles          --&gt; gin-blog/routers/api/v1.GetArticles (3 handlers)
[GIN-debug] GET    /api/v1/articles/:id      --&gt; gin-blog/routers/api/v1.GetArticle (3 handlers)
[GIN-debug] POST   /api/v1/articles          --&gt; gin-blog/routers/api/v1.AddArticle (3 handlers)
[GIN-debug] PUT    /api/v1/articles/:id      --&gt; gin-blog/routers/api/v1.EditArticle (3 handlers)
[GIN-debug] DELETE /api/v1/articles/:id      --&gt; gin-blog/routers/api/v1.DeleteArticle (3 handlers)
</code></pre><p>使用<code>Postman</code>检验接口是否正常，在这里大家可以选用合适的参数传递方式，此处为了方便展示我选用了 GET/Param 传参的方式，而后期会改为 POST。</p>
<ul>
<li>POST：http://127.0.0.1:8000/api/v1/articles?tag_id=1&amp;title=test1&amp;desc=test-desc&amp;content=test-content&amp;created_by=test-created&amp;state=1</li>
<li>GET：http://127.0.0.1:8000/api/v1/articles</li>
<li>GET：http://127.0.0.1:8000/api/v1/articles/1</li>
<li>PUT：http://127.0.0.1:8000/api/v1/articles/1?tag_id=1&amp;title=test-edit1&amp;desc=test-desc-edit&amp;content=test-content-edit&amp;modified_by=test-created-edit&amp;state=0</li>
<li>DELETE：http://127.0.0.1:8000/api/v1/articles/1</li>
</ul>
<p>至此，我们的 API&rsquo;s 编写就到这里，下一节我们将介绍另外的一些技巧！</p>
<h2 id="参考">参考</h2>
<h3 id="本系列示例代码">本系列示例代码</h3>
<ul>
<li><a href="https://github.com/EDDYCJY/go-gin-example">go-gin-example</a></li>
</ul>
<h2 id="关于">关于</h2>
<h3 id="修改记录">修改记录</h3>
<ul>
<li>第一版：2018 年 02 月 16 日发布文章</li>
<li>第二版：2019 年 10 月 01 日修改文章</li>
</ul>
<h2 id="heading">？</h2>
<p>如果有任何疑问或错误，欢迎在 <a href="https://github.com/EDDYCJY/blog">issues</a> 进行提问或给予修正意见，如果喜欢或对你有所帮助，欢迎 Star，对作者是一种鼓励和推进。</p>
<h3 id="我的公众号">我的公众号</h3>
<p><img src="https://www.lican.site/wechat/lican.png" alt="image"></p>


                                        
                                        
                                        
                                        <div class="rp4wp-related-posts">
                                            <h3>相关文章</h3>
                                            <ul>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.asia/posts/posts/go/gin/2018-02-12-api-02/">「连载三」Gin搭建Blog API&#39;s （二）</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.asia/posts/posts/go/gin/2018-02-11-api-01/">「连载二」Gin搭建Blog API&#39;s （一）</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.asia/posts/posts/go/gin/2018-02-10-install/">「连载一」Go 介绍与环境安装</a>
                                                    </div>
                                                </li>
                                                
                                            </ul>
                                        </div>
                                        
                                        
                                    </div>

                                    
                                    
                                    

                                    
                                    <div class="post-meta">
                                        
                                        
                                        <div class="post-tags">
                                            <ul>
                                            
                                            <li>
                                                <a href="/tags/go" title="View all posts tagged match">go</a>
                                            </li>
                                            
                                            <li>
                                                <a href="/tags/gin" title="View all posts tagged match">gin</a>
                                            </li>
                                            
                                            </ul>
                                        </div>
                                        
                                        
                                        <nav class="further-reading">
                                            
                                            <div class="previous">
                                                <span>&lt;&lt; Prev</span>
                                                <a href="https://www.lican.asia/posts/posts/go/gin/2018-02-12-api-02/"
                                                    rel="prev">「连载三」Gin搭建Blog API&#39;s （二）</a> </div>
                                            
                                            
                                            <div class="next">
                                                <span>Next >></span>
                                                <a href="https://www.lican.asia/posts/posts/go/gin/2018-02-14-jwt/">「连载五」使用 JWT 进行身份校验</a> 
                                            </div>
                                            
                                        </nav>
                                    </div>
                                    
                                    

                                    
                                    
                                    

                                </div>
                            </article>
                        </div>
                    </div>
                </section>
                <aside class="sidebar sidebar-primary" id="sidebar-primary" role="complementary">
    <h1 class="screen-reader-text">Sidebar</h1>
    

    
    
    <section id="text-2" class="widget widget_text">
        <div class="textwidget">
            
            <div id="profile">
                <div id="profile_picture"><img src="https://www.lican.asia/logo/logo.png"></div>
                <div id="profile_intro">
                    <p><span class="name">Lican</span></p>
                    <p class="intro">全栈开发者，爱好造轮子。</p>
                </div>
            </div>
            
            <p>
                <script type="text/javascript">
                    (function ($) {
                        $(document).ready(function () {
                            var menuPrimaryContainer = $('#menu-primary-container');
                            var profile = $('#text-2');
                            $('#toggle-navigation').click(function () {
                                if (menuPrimaryContainer.hasClass('open')) {
                                    profile.removeClass('open');
                                } else {
                                    profile.addClass('open');
                                }
                            });
                        });
                    })(jQuery);
                </script>
            </p>
        </div>
    </section>
    
    
    
    
    
    <section id="text-5" class="widget widget_text">
        <h2 class="widget-title">开源项目</h2>
        <div class="textwidget">
            <div id="projects" style="line-height: 22px;">
                
                <a href="https://github.com/idoubi/gonews"
                    target="_blank">gonews</a>: &nbsp;Daily news for golang<br>
                
                <a href="https://github.com/idoubi/sql2struct"
                    target="_blank">sql2struct</a>: &nbsp;Generate go struct according to SQL<br>
                
                <a href="https://github.com/idoubi/goz"
                    target="_blank">goz</a>: &nbsp;Request library used in golang<br>
                
        </div>
    </section>
    
    

    
    
    
    
    <section id="qrcode" class="widget widget_media_image">
        <h2 class="widget-title">微信公众号</h2>
        <img width="258" height="258"
            src="https://www.lican.asia/wechat/lican.png"
            class="image wp-image-5514  attachment-full size-full" alt=""
            style="max-width: 100%; height: auto;"
            sizes="(max-width: 258px) 100vw, 258px">
    </section>
    
    

    
    
    

    
    
    

    
    
    
    
</aside>
            </div>
        </div>

        <footer id="site-footer" class="site-footer" role="contentinfo">
    <div class="max-width">
    </div>
    <div class="footer">
        <div id="footercontent">
            © lican.asia All rights reserved<br/>
            Built with Hugo Theme <a href="https://github.com/idoubi/hugo-theme-period" target="_blank">Period</a>
        </div>
    </div>
</footer>

<script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?e8351b6d4626d5881d439ea1f6184baa";
      var s = document.getElementsByTagName("script")[0]; 
      s.parentNode.insertBefore(hm, s);
    })();
</script>
    
    
    </div>
    
</body>

</html>